package com.blog.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.blog.entity.PageResult;
import com.blog.mapper.TbBlogMapper;
import com.blog.pojo.TbBlog;
import com.blog.pojo.TbBlogExample;
import com.blog.pojo.TbBlogExample.Criteria;
import com.blog.service.BlogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class BlogServiceImpl implements BlogService {

	@Autowired
	private TbBlogMapper tbBlogMapper;

	@Autowired
	private RedisTemplate redisTemplate;

	public TbBlog getBlogById(Integer id) {
		return tbBlogMapper.selectByPrimaryKey(id);
	}

	public PageResult getBlogList(int page, int rows, TbBlog blog) {
			
		
		// 如果没有搜索条件,才从缓存中读取数据
		if ((blog!=null&&blog.getTitle()==null||blog.getTitle()=="")||blog==null) {
			// 从缓存中取数据
			PageResult result = (PageResult) redisTemplate.boundHashOps("blogList").get(page);
			// 如果缓存为空
			if (result == null || result.getList() == null || result.getList().size() < 0) {
				PageHelper.startPage(page, rows, "createDate desc");
				TbBlogExample example = new TbBlogExample();
				Criteria criteria = example.createCriteria();
				List<TbBlog> blogList = tbBlogMapper.selectByExample(example);
				PageInfo<TbBlog> pageInfo = new PageInfo<TbBlog>(blogList);
				PageResult pageResult = new PageResult();
				pageResult.setCurrentPage(pageInfo.getPageNum());
				pageResult.setTotal((int) pageInfo.getTotal());
				pageResult.setList(blogList);
				pageResult.setPageNum(pageInfo.getPages());
				// 将结果存入缓存
				redisTemplate.boundHashOps("blogList").put(page, pageResult);
				return pageResult;
			}
			return result;
		}else {
			PageHelper.startPage(page, rows, "createDate desc");
			TbBlogExample example = new TbBlogExample();
			Criteria criteria = example.createCriteria();
			criteria.andTitleLike("%" + blog.getTitle() + "%");
			List<TbBlog> blogList = tbBlogMapper.selectByExample(example);
			PageInfo<TbBlog> pageInfo = new PageInfo<TbBlog>(blogList);
			PageResult pageResult = new PageResult();
			pageResult.setCurrentPage(pageInfo.getPageNum());
			pageResult.setTotal((int) pageInfo.getTotal());
			pageResult.setList(blogList);
			pageResult.setPageNum(pageInfo.getPages());
			return pageResult;
		}

	}

	public PageResult getBlogListByTypeId(int page, int rows, Integer typeId) {
		PageHelper.startPage(page, rows, "createDate desc");
		TbBlogExample example = new TbBlogExample();
		Criteria criteria = example.createCriteria();
		criteria.andTypeidEqualTo(typeId);
		List<TbBlog> blogList = tbBlogMapper.selectByExample(example);
		PageInfo<TbBlog> pageInfo = new PageInfo<TbBlog>(blogList);
		PageResult pageResult = new PageResult();
		pageResult.setCurrentPage(pageInfo.getPageNum());
		pageResult.setTotal((int) pageInfo.getTotal());
		pageResult.setList(blogList);
		pageResult.setPageNum(pageInfo.getPages());
		return pageResult;
	}

}
